package com.hy.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hy.ruiji.common.R;
import com.hy.ruiji.common.Status;
import com.hy.ruiji.common.customException;
import com.hy.ruiji.enyity.AddressBook;
import com.hy.ruiji.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 地址保存
     * @param addressBook 对地址进行保存
     * @return 返回保存结果
     */
    @PostMapping
    public R<String> saveAddressBook(@RequestBody AddressBook addressBook){
        //插入用户id,调用公共方法填充id
        addressBook.setUserId(Status.getCurrentId());
        log.info(addressBook.toString());
        //保存到数据库
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }

    /**
     * 查询登录用户的地址信息
     * @param addressBook 里面封装了用户的id
     * @return 返回用户的地址集合
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook){
        addressBook.setUserId(Status.getCurrentId());
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        //只查询当前用户登录的地址信息
        wrapper.eq(addressBook.getUserId()!=null,AddressBook::getUserId,Status.getCurrentId());
        wrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(wrapper);
        return R.success(list);
    }

    /**
     * 根据id查询地址的信息
     * @param userId 传入的用户id
     * @return 返回查询结果
     */
    @GetMapping("{userId}")
    public R<AddressBook> queryById(@PathVariable Long userId){
        if(userId==null){
            return R.error("用户未登录");
        }
        AddressBook addressBook = addressBookService.getById(userId);
        return R.success(addressBook);
    }

    /**
     * 根据用户地址id修改地址信息
     * @param addressBook 需要修改的地址信息
     * @return 返回修改的结果
     */
    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook){
        addressBook.setUserId(Status.getCurrentId());
        addressBookService.updateById(addressBook);
        return R.success("修改成功！");
    }

    /**
     * 删除地址
     * @param ids 需要删除的地址id
     * @return 返回删除结果
     */
    @DeleteMapping
    public R<String> DeleteAddressBook(Long ids){
        if(ids==null){
            throw new customException("删除失败");
        }
        addressBookService.removeById(ids);
        return R.success("删除成功！");
    }

    /**
     *查询默认地址
     * @return 返回默认地址信息
     */
    @GetMapping("default")
    public R<AddressBook> getDefault(){
        //条件构造器
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,Status.getCurrentId());
        wrapper.eq(AddressBook::getIsDefault,1);
        //查询数据库
        AddressBook addressBook = addressBookService.getOne(wrapper);
        if(addressBook==null){
            return R.error("没要找到，似乎没有设置");
        }
        return R.success(addressBook);
    }
    /**
     * 设置默认地址
     */
    @PutMapping("default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook){
        //设置条件构造器，把所有的默认地址值都设为0
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        //获取当前用户的所有地址
        wrapper.eq(AddressBook::getUserId,Status.getCurrentId());
        //把默认地址都改为0
        wrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(wrapper);

        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }
}
